package com.ccw.file;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class SuperFileUtils {

	private static BufferedReader br;

	public static String findFile(String fileName, String path) {
		File dir = new File(path);
		String filePath = null;
		if (!dir.exists() || !dir.isDirectory()) {
			return filePath;
		}
		File[] dirfiles = dir.listFiles();
		for (File file : dirfiles) {
			if (file.isDirectory()) {
				continue;
			}
			if (file.getName().equals(fileName)) {
				filePath = file.getPath();
				break;
			}
		}
		return filePath;
	}

	public static String getFileLineString(String filePath)
			throws IOException {
		br = new BufferedReader(new FileReader(filePath));
		StringBuilder sb = new StringBuilder();
		String r = br.readLine();
		while (r != null) {
			sb.append(r);
			sb.append("\r\n");
			r = br.readLine();
		}
		br.close();
		br = null;
		return sb.toString();
	}

	public static List<String> getFileLineList(String filePath)
			throws IOException {
		br = new BufferedReader(new FileReader(filePath));
		List<String> lines = new ArrayList<String>();
		String r = br.readLine();
		while (r != null) {
			lines.add(r);
			r = br.readLine();
		}
		br.close();
		br = null;
		return lines;
	}

	public static String[] getFileLineArray(String filePath) throws IOException {
		return changeListToArray(getFileLineList(filePath));
	}

	public static String[] changeListToArray(List<String> lines) {
		if (lines != null) {
			return lines.toArray(new String[0]);
		}
		return new String[0];
	}

	public static List<String> getFileLineListBetweenMark(String filePath,
			String mark) throws IOException {
		br = new BufferedReader(new FileReader(filePath));
		List<String> lines = new ArrayList<String>();
		String r = br.readLine();
		boolean flag = false;
		while (r != null) {
			if (!flag && r.indexOf(mark) >= 0) {
				flag = true;
				r = br.readLine();
				continue;
			}
			if (flag && r.indexOf(mark) >= 0) {
				break;
			}
			if (flag) {
				lines.add(r);
			}
			r = br.readLine();
		}
		br.close();
		br = null;
		return lines;
	}

	public static List<String> getFileLineListBetweenIncludeMark(
			String filePath, String mark) throws IOException {
		br = new BufferedReader(new FileReader(filePath));
		List<String> lines = new ArrayList<String>();
		String r = br.readLine();
		boolean flag = false;
		while (r != null) {
			if (flag && r.indexOf(mark) >= 0) {
				lines.add(r);
				// System.out.println(r);
				break;
			}
			if (!flag && r.indexOf(mark) >= 0) {
				flag = true;
			}
			if (flag) {
				lines.add(r);
				// System.out.println(r);
			}
			r = br.readLine();
		}
		br.close();
		br = null;
		return lines;
	}

	public static List<String> getFileLineListBeforeMark(String filePath,
			String mark) throws IOException {
		br = new BufferedReader(new FileReader(filePath));
		List<String> lines = new ArrayList<String>();
		String r = br.readLine();
		while (r != null) {
			if (r.indexOf(mark) >= 0) {
				break;
			}
			lines.add(r);
			// System.out.println(r);
			r = br.readLine();
		}
		br.close();
		br = null;
		return lines;
	}

	public static List<String> getFileLineListBeforeIncludeMark(
			String filePath, String mark) throws IOException {
		br = new BufferedReader(new FileReader(filePath));
		List<String> lines = new ArrayList<String>();
		String r = br.readLine();
		while (r != null) {
			lines.add(r);
			// System.out.println(r);
			if (r.indexOf(mark) >= 0) {
				break;
			}
			r = br.readLine();
		}
		br.close();
		br = null;
		return lines;
	}

	public static List<String> getFileLineListOutOfMark(String filePath,
			String mark) throws IOException {
		br = new BufferedReader(new FileReader(filePath));
		List<String> lines = new ArrayList<String>();
		String r = br.readLine();
		boolean flag = true;
		while (r != null) {
			if (r.indexOf(mark) >= 0) {
				if (flag) {
					flag = false;
				} else {
					flag = true;
					r = br.readLine();
				}
			}
			if (flag) {
				lines.add(r);
				// System.out.println(r);
			}
			r = br.readLine();
		}
		br.close();
		br = null;
		return lines;
	}

	public static List<String> getFileLineListOutOfIncludeMark(String filePath,
			String mark) throws IOException {
		br = new BufferedReader(new FileReader(filePath));
		List<String> lines = new ArrayList<String>();
		String r = br.readLine();
		boolean flag = true;
		while (r != null) {
			if (r.indexOf(mark) >= 0) {
				if (flag) {
					flag = false;
					lines.add(r);
					// System.out.println(r);
				} else {
					flag = true;
				}
			}
			if (flag) {
				lines.add(r);
				// System.out.println(r);
			}
			r = br.readLine();
		}
		br.close();
		br = null;
		return lines;
	}

	/**
	 * write all the lines into one file
	 * 
	 * @param coreLines
	 * @param filePath
	 * @throws Exception
	 */
	public static void buildXml(List<String> coreLines, String filePath)
			throws Exception {
		System.out.println("build file:" + filePath);
		File file = new File(filePath);
		if (!file.exists()) {
			try {
				if (!file.getParentFile().exists()) {
					file.getParentFile().mkdirs();
				}
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			file.delete();
			file.createNewFile();
		}
		BufferedWriter writer = new BufferedWriter(new FileWriter(filePath));
		for (String line : coreLines) {
			writer.write(line + "\r\n");
		}
		writer.close();
	}

	public static List<String> getFileLineListAfterMark(String filePath,
			String mark) throws IOException {
		br = new BufferedReader(new FileReader(filePath));
		List<String> lines = new ArrayList<String>();
		String r = br.readLine();
		boolean flag = false;
		while (r != null) {
			if (!flag && r.indexOf(mark) >= 0) {
				flag = true;
				r = br.readLine();
				continue;
			}
			if (flag) {
				lines.add(r);
				// System.out.println(r);
			}
			r = br.readLine();
		}
		br.close();
		br = null;
		return lines;
	}

	public static List<String> getFileLineListAfterIncludeMark(String filePath,
			String mark) throws IOException {
		br = new BufferedReader(new FileReader(filePath));
		List<String> lines = new ArrayList<String>();
		String r = br.readLine();
		boolean flag = false;
		while (r != null) {
			if (!flag && r.indexOf(mark) >= 0) {
				flag = true;
			}
			if (flag) {
				lines.add(r);
				// System.out.println(r);
			}
			r = br.readLine();
		}
		br.close();
		br = null;
		return lines;
	}

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws Exception {
		String filePath = "C:\\ccwang\\workspace_gpm\\GPM_SOURCE_BUILD\\config\\extension\\spring-enhancement-dao.xml";
		getFileLineListBeforeMark(filePath, "custom add start");
		System.out.println("------------------------------------------------");
		getFileLineListBeforeIncludeMark(filePath, "custom add start");
		System.out.println("------------------------------------------------");
		getFileLineListAfterMark(filePath, "custom add start");
		System.out.println("------------------------------------------------");
		getFileLineListAfterIncludeMark(filePath, "custom add start");
		System.out.println("------------------------------------------------");
	}

}
